home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / CLOBSH.PAK / LIST.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  4KB  |  193 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  LIST.H                                                                */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991, 1993                            */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __LIST_H )
  11. #define __LIST_H
  12.  
  13. #define BI_OLDNAMES
  14.  
  15. #if !defined( __MEMMGR_H )
  16. #include "classlib\MemMgr.h"
  17. #endif  // __MEMMGR_H
  18.  
  19. #if !defined( __COLLECT_H )
  20. #include "classlib\obsolete\Collect.h"
  21. #endif  // __COLLECT_H
  22.  
  23. #pragma option -Vo-
  24. #if defined( __BCOPT__ ) && !defined( __FLAT__ ) && !defined( _ALLOW_po )
  25. #pragma option -po-
  26. #endif
  27.  
  28. _CLASSDEF(List)
  29. _CLASSDEF(ListIterator)
  30.  
  31. class _CLASSTYPE ListBlockInitializer
  32. {
  33.  
  34. protected:
  35.  
  36.     ListBlockInitializer();
  37.     ~ListBlockInitializer();
  38.  
  39.     static unsigned count;
  40.  
  41. };
  42.  
  43. class _CLASSTYPE List : public Collection, private ListBlockInitializer
  44. {
  45.  
  46. public:
  47.  
  48.     List() :
  49.         headEntry( 0, &tailEntry ),
  50.         tailEntry( 0, &tailEntry ),
  51.         head(&headEntry),
  52.         tail(&tailEntry),
  53.         itemsInContainer(0)
  54.         {
  55.         }
  56.  
  57.     virtual ~List()
  58.         {
  59.         flush();
  60.         }
  61.     
  62.     Object _FAR & peekHead() const
  63.         {
  64.         return ptrToRef(head->next->data);
  65.         }
  66.  
  67.     void add( Object _FAR & );
  68.     virtual void detach( Object _FAR &, DeleteType = NoDelete );
  69.     virtual void flush( DeleteType = DefDelete );
  70.  
  71.     virtual int isEmpty() const
  72.         {
  73.         return itemsInContainer == 0;
  74.         }
  75.  
  76.     virtual countType getItemsInContainer() const
  77.         {
  78.         return itemsInContainer;
  79.         }
  80.  
  81.     virtual ContainerIterator _FAR & initIterator() const;
  82.  
  83.     virtual classType isA() const
  84.         {
  85.         return listClass;
  86.         }
  87.  
  88.     virtual _TCHAR _FAR *nameOf() const
  89.         {
  90.         return "List";
  91.         }
  92.  
  93. private:
  94.  
  95.     class _CLASSTYPE ListElement
  96.     {
  97.  
  98.     public:
  99.  
  100.         ListElement( Object _FAR *o, ListElement _FAR *n = 0 )
  101.             {
  102.             data = o; next = n;
  103.             }
  104.  
  105.     private:
  106.  
  107.         ListElement _FAR *next;
  108.         Object _FAR *data;
  109.  
  110.         void _FAR *operator new( size_t sz )
  111.             {
  112.             PRECONDITION( mgr != 0 );
  113.             return mgr->allocate( sz );
  114.             }
  115.         void operator delete( void _FAR *b )
  116.             {
  117.             PRECONDITION( mgr != 0 );
  118.             mgr->free( b );
  119.             }
  120.  
  121.         static MemBlocks _FAR *mgr;
  122.  
  123.         friend class List;
  124.         friend class ListIterator;
  125.         friend class ListBlockInitializer;
  126.  
  127.     };
  128.  
  129.     ListElement _FAR *head;
  130.     ListElement _FAR *tail;
  131.  
  132.     ListElement headEntry, tailEntry;
  133.  
  134.     unsigned itemsInContainer;
  135.  
  136.     ListElement _FAR *findPred( const Object _FAR & o );
  137.  
  138.     friend class ListIterator;
  139.     friend class ListBlockInitializer;
  140.  
  141. };
  142.  
  143. inline ListBlockInitializer::ListBlockInitializer()
  144. {
  145.     PRECONDITION( count != UINT_MAX );
  146.     if( count++ == 0 )
  147.         List::ListElement::mgr = 
  148.             new MemBlocks( sizeof(List::ListElement), 20 );
  149. }
  150.  
  151. inline ListBlockInitializer::~ListBlockInitializer()
  152. {
  153.     PRECONDITION( count != 0 );
  154.     if( --count == 0 )
  155.         {
  156.         delete List::ListElement::mgr;
  157.         List::ListElement::mgr = 0;
  158.         }
  159. }
  160.  
  161. class _CLASSTYPE ListIterator : public ContainerIterator
  162. {
  163.  
  164. public:
  165.  
  166.     ListIterator( const List _FAR & );
  167.     virtual ~ListIterator();
  168.  
  169.     virtual operator int();
  170.     virtual Object _FAR & current();
  171.     virtual Object _FAR & operator ++ ( int );
  172.     virtual Object _FAR & operator ++ ();
  173.     virtual void restart();
  174.  
  175. private:
  176.  
  177.     List::ListElement _FAR *currentElement;
  178.     List::ListElement _FAR *startingElement;
  179. };
  180.  
  181. inline ListIterator::ListIterator( const List _FAR & toIterate )
  182. {
  183.     startingElement = currentElement = toIterate.head->next;
  184. }
  185.  
  186. #if defined( __BCOPT__ ) && !defined( __FLAT__ ) && !defined( _ALLOW_po )
  187. #pragma option -po.
  188. #endif
  189. #pragma option -Vo.
  190.  
  191. #endif  // __LIST_H
  192.  
  193.